class AdminController < ApplicationController
  
  before_filter :authorize
  
  def index
    load
  end
  
  def save_password
    if request.post?
      @current_user.password = params[:password]
      @current_user.password_confirmation = params[:password_confirmation]
      if @current_user.save
        flash[:setting_notice] = "密码修改成功。"
      end
    end
    load
    render(:action => :index)
  end
  
  def save_setting
    if request.post?
      font_family = params[:setting][:font_family]
      font_size = params[:setting][:font_size]
      #current_user = User.find_by_username(session[:user_id])
      @current_user.set_setting('font_family', font_family)
      @current_user.set_setting('font_size', font_size)
    end
    load
    render(:action => :index)
  end
  
  def delete_user
    if request.delete?
      user = User.find(params[:id])
      begin
        user.destroy
        flash[:user_notice] = "用户 \"#{user.name}\"删除了。"
      rescue Exception => e
        flash[:user_notice] = e.message
      end
    end
    load
    render(:action => :index)
  end
  
  def create_user
    @new_user = User.new(params[:new_user])
    if request.post? and @new_user.save
      flash[:user_notice] = "用户 \"#{@new_user.name}\"创建了。"
      @new_user = User.new
    end
    load
    render(:action => :index)
  end
  
  private
  def load
    @users = User.find(:all)
    #@current_user = User.find_by_username(session[:user_id])
    @font_size = @current_user.get_setting('font_size')
    @font_family = @current_user.get_setting('font_family')
  end
end
